package com.horoscope.astrology.zodiac.signs.free.activity

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.bean.QuizBean
import com.horoscope.astrology.zodiac.signs.free.bean.ResultsBean
import com.horoscope.astrology.zodiac.signs.free.utils.ImageLoader
import com.horoscope.astrology.zodiac.signs.free.utils.ShareManager
import com.horoscope.astrology.zodiac.signs.free.utils.SysUtils
import com.horoscope.astrology.zodiac.signs.free.utils.ToastUtil
import kotlinx.android.synthetic.main.back_title.*
import kotlinx.android.synthetic.main.item_share.*
import java.util.*

/**
 * quizzes 结果页
 */
class ResultActivity : Base2Activity() {

    private var ivIcon: ImageView? = null
    private var btBack: ImageView? = null
    private var tv: TextView? = null
    private var tryAgain: TextView? = null
    private var ac: ResultActivity? = null
    private var mBean = QuizBean()

    override fun onCreate(savedInstanceState: Bundle?) {
        immersionWhite()
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_result2)
        initTitleBar(
            this@ResultActivity,
            R.id.btn_back_edit_name,
            R.id.text_view_back_title,
            getString(R.string.label_result)
        )
        text_view_back_title.setTextColor(Color.BLACK)
        btn_back_edit_name.setImageResource(R.drawable.icon_back_black)
        ac = this
        intent.getSerializableExtra("mBean")?.let {
            mBean = it as QuizBean
        }
        initView()
        initPage()
    }

    private fun initPage() {
        if (intent == null) return
        if (intent.extras == null) return

        val index = intent.getIntExtra("num", 0)
        var bean: ResultsBean? = null
        bean = if (mBean.results.size > 0 && index < mBean.results.size) {
            mBean.results[index]
        } else if (mBean.results.size > 0) {
            mBean.results[0]
        } else {
            ToastUtil.showText(this, "No results were obtained!!!")
            return
        }
        tv!!.text = bean.text
        ImageLoader.loadImage(ivIcon!!, bean.imageUrl)
    }

    private fun initView() {
        ivIcon = findViewById(R.id.resAc_iv_1)
        tv = findViewById(R.id.resAc_tv)
        tryAgain = findViewById(R.id.resAc_try_again)

        tryAgain!!.setOnClickListener {
            if (SysUtils.isFastClick()) return@setOnClickListener
            finish()
            this.startActivity(QuizActivity.generateIntent(this@ResultActivity, mBean))
        }
        ShareManager.initShare(
            this@ResultActivity,
            line_left,
            line_right,
            text_view_share,
            image_view_more_share,
            tv_facebook
        )
    }

    companion object {

        fun generateIntent(context: Context, mBean: QuizBean, resultNumber: Int): Intent {
            val intent = Intent(context, ResultActivity::class.java)
            intent.putExtra("mBean", mBean)
            intent.putExtra("num", resultNumber)
            return intent
        }
    }
}
